﻿using Ec.NETCoreFramework.Common.Core;
using Ec.NETCoreFramework.Common.Core.Helpers;
using Ec.NETCoreFramework.Common.MQ;
using Ec.NETCoreFramework.Common.Core.Utilities;
using Ec.NETCoreFramework.Common.Core.Serializer;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Threading;
using System.Diagnostics;
using Ec.NETCoreFramework.Common.Plug.Core;
using Ec.NETCoreFramework.Common.Clients;
using System.Linq;

namespace MQ_Approval
{
    /// <summary>
    /// 汇报
    /// </summary>
    public class Approval : TaskInterface
    {
        private  ConsumeConfig _consumeConfig;
        public override async Task ConsumeEvent(byte[] bytes)
        {

            var dic = ByteSerializer.Deserialize<Dictionary<string, object>>(bytes);
            var actoin = ApprovalEnum.TimingSync.ToString();
            if (dic != null)
            {
                actoin = Utils.ToString(dic["action"]);
            }
            ApprovalSync bll = null;
            var appSecret = new RequestAppSecret()
            {
                AppId = _consumeConfig.AppId,
                Secret = _consumeConfig.Secret
            };
            if (actoin == ApprovalEnum.ManualSync.ToString())
            {
                bll = new ManualSyncApproval(appSecret,_consumeConfig);
            }
            else if(actoin == ApprovalEnum.TimingSync.ToString())
            {
                bll = new TimingSyncApproval(appSecret, _consumeConfig);
            }
            if (bll != null)
            {
                await bll.Excute(dic);
            }
        }

       
        private string _QueueName = string.Empty;
        public override string QueueName()
        {
            return "wechat-" + _consumeConfig.MQName;
        }
        public override void SetConsumeConfig(byte[] consumeConfig)
        {
            _consumeConfig = ProtoBufSerializer.DeSerialize<ConsumeConfig>(consumeConfig);
        }
    }

}
